/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prerecitran.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedActual;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedEstimated;
import com.jeesite.modules.prerecieve.service.TadvancesReceivedService;
import com.jeesite.modules.prerecitran.entity.TadvancesReceivedTransferDetail;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.prerecitran.entity.TadvancesReceivedTransfer;
import com.jeesite.modules.prerecitran.service.TadvancesReceivedTransferService;

import java.util.ArrayList;
import java.util.List;

/**
 * 预收转账单Controller
 * @author wsm
 * @version 2021-03-24
 */
@Controller
@RequestMapping(value = "${adminPath}/prerecitran/tadvancesReceivedTransfer")
public class TadvancesReceivedTransferController extends BaseController {

	@Autowired
	private TadvancesReceivedTransferService tadvancesReceivedTransferService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private TadvancesReceivedService tadvancesReceivedService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private TcarService tcarService;
	@Autowired
	private FixedExpenseService fixedExpenseService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TadvancesReceivedTransfer get(String advancesReceivedTransferId, boolean isNewRecord) {
		return tadvancesReceivedTransferService.get(advancesReceivedTransferId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:view")
	@RequestMapping(value = {"list", ""})
	public String list(TadvancesReceivedTransfer tadvancesReceivedTransfer, Model model) {
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList", serviceOrderList);
		//客户
		List<Customer> customerList = customerService.findList(new Customer());
		model.addAttribute("customerList", customerList);
		//预收单
		List<TadvancesReceived> advancesReceivedList = tadvancesReceivedService.findList(new TadvancesReceived());
		model.addAttribute("advancesReceivedList", advancesReceivedList);
		model.addAttribute("tadvancesReceivedTransfer", tadvancesReceivedTransfer);
		return "modules/prerecitran/tadvancesReceivedTransferList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TadvancesReceivedTransfer> listData(TadvancesReceivedTransfer tadvancesReceivedTransfer, HttpServletRequest request, HttpServletResponse response) {
		tadvancesReceivedTransfer.setPage(new Page<>(request, response));
		Page<TadvancesReceivedTransfer> page = tadvancesReceivedTransferService.findPage(tadvancesReceivedTransfer);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:view")
	@RequestMapping(value = "form")
	public String form(TadvancesReceivedTransfer tadvancesReceivedTransfer, Model model) {
		if(!tadvancesReceivedTransfer.getIsNewRecord()){
			for (TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail : tadvancesReceivedTransfer.getTadvancesReceivedTransferDetailList()) {
				tadvancesReceivedTransferDetail.setCarName(tadvancesReceivedTransferDetail.getTcar().getCarName());
				tadvancesReceivedTransferDetail.setOriginalExpenseTypes(tadvancesReceivedTransferDetail.getOriginalExpenseType1().getFixedExpenseName());
			}
		}
		if(tadvancesReceivedTransfer.getIsNewRecord()){
			//预收单
			List<TadvancesReceived> advancesReceivedList = tadvancesReceivedService.findList(new TadvancesReceived());
			model.addAttribute("advancesReceivedList", advancesReceivedList);
		}else {
			//预收单
			TadvancesReceived tadvancesReceived =new TadvancesReceived();
			tadvancesReceived.setAdvancesReceivedCode(tadvancesReceivedTransfer.getAdvancesReceivedCode());
			tadvancesReceived =tadvancesReceivedService.findList(tadvancesReceived).get(0);
			tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);
			List<TadvancesReceived> advancesReceivedList = new ArrayList<>();
			advancesReceivedList.add(tadvancesReceived);
			model.addAttribute("advancesReceivedList", advancesReceivedList);
		}

		model.addAttribute("tadvancesReceivedTransfer", tadvancesReceivedTransfer);
		return "modules/prerecitran/tadvancesReceivedTransferForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:view")
	@RequestMapping(value = "detail")
	public String detail(TadvancesReceivedTransfer tadvancesReceivedTransfer, Model model) {
			ServiceOrder serviceOrder =new ServiceOrder();
			serviceOrder.setServiceOrderId(tadvancesReceivedTransfer.getServiceOrderId());
			serviceOrder=serviceOrderService.get(serviceOrder);
			//服务订单
			List<ServiceOrder> serviceOrderList = new ArrayList<>();
			serviceOrderList.add(serviceOrder);
			model.addAttribute("serviceOrderList", serviceOrderList);

			//客户
			Customer customer =new Customer();
			customer.setCustomerId(tadvancesReceivedTransfer.getCustomerId());
			customer=customerService.get(customer);
			List<Customer> customerList = new ArrayList<>();
			customerList.add(customer);
			model.addAttribute("customerList", customerList);

			//预收单
			TadvancesReceived tadvancesReceived =new TadvancesReceived();
			tadvancesReceived.setAdvancesReceivedCode(tadvancesReceivedTransfer.getAdvancesReceivedCode());
			tadvancesReceived =tadvancesReceivedService.findList(tadvancesReceived).get(0);
			tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);
			List<TadvancesReceived> advancesReceivedList = new ArrayList<>();
			advancesReceivedList.add(tadvancesReceived);
			model.addAttribute("advancesReceivedList", advancesReceivedList);

			model.addAttribute("tadvancesReceivedTransfer", tadvancesReceivedTransfer);
			return "modules/prerecitran/tadvancesReceivedTransferDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		//通过预收转账单得到服务单
		TadvancesReceived tadvancesReceived =new TadvancesReceived();
		//code实际得到预收款下拉框的value值是Id
		tadvancesReceived.setAdvancesReceivedId(tadvancesReceivedTransfer.getAdvancesReceivedCode());
		tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);

		//设置订单与客户
		tadvancesReceivedTransfer.setCustomerId(tadvancesReceived.getCustomerId());
		tadvancesReceivedTransfer.setServiceOrderId(tadvancesReceived.getServiceOrderId());
		tadvancesReceivedTransfer.setAdvancesReceivedCode(tadvancesReceived.getAdvancesReceivedCode());
		//存放子表
		List<TadvancesReceivedTransferDetail> tadvancesReceivedTransferDetails =new ArrayList<>();
		for (TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail : tadvancesReceivedTransfer.getTadvancesReceivedTransferDetailList()) {
			//判断有无修改预收转账
			if(!"".equals(tadvancesReceivedTransferDetail.getExpenseType())&&!"".equals(tadvancesReceivedTransferDetail.getRowNumber())&&tadvancesReceivedTransferDetail.getCarryOverAmount()!=null){
					//修改锁定金额，可用金额
				for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {
					if(tadvancesReceivedEstimated.getRowNumber().equals(tadvancesReceivedTransferDetail.getOriginRowNumber())){
						tadvancesReceivedEstimated.setUsedAmount(tadvancesReceivedEstimated.getUsedAmount()-tadvancesReceivedTransferDetail.getCarryOverAmount());
						tadvancesReceivedEstimated.setLockedAmount(tadvancesReceivedEstimated.getLockedAmount()+tadvancesReceivedTransferDetail.getCarryOverAmount());
					}
				}
				     tadvancesReceivedTransferDetails.add(tadvancesReceivedTransferDetail);
				}
			}
		//设置子表的数据（只修改的那些）
		tadvancesReceivedTransfer.setTadvancesReceivedTransferDetailList(tadvancesReceivedTransferDetails);
		tadvancesReceivedService.save(tadvancesReceived);
		tadvancesReceivedTransferService.save(tadvancesReceivedTransfer);
		return renderResult(Global.TRUE, text("保存预收转账单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TadvancesReceivedTransfer tadvancesReceivedTransfer) {
		tadvancesReceivedTransferService.delete(tadvancesReceivedTransfer);
		return renderResult(Global.TRUE, text("删除预收转账单成功！"));
	}
	/*
	* 获得对应的预收单
	* */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:view")
	@RequestMapping(value = "getPreReic")
	@ResponseBody
	public TadvancesReceived getPreReic(String id){
		TadvancesReceived tadvancesReceived =new TadvancesReceived();
		tadvancesReceived.setAdvancesReceivedId(id);
		tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);

		//服务订单查
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(tadvancesReceived.getServiceOrderId());
		serviceOrder=serviceOrderService.get(serviceOrder);


		for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
			for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {

				//订单详情和预收单详情通过对比行号确定
				if(serviceOrderDetail.getRowNumber().equals(tadvancesReceivedEstimated.getRowNumber())){

					tadvancesReceivedEstimated.setCarId(serviceOrderDetail.getCarId());
					tadvancesReceivedEstimated.setAdvancesReceivedTypeName(tadvancesReceivedEstimated.getFixedExpense().getFixedExpenseName());
					tadvancesReceivedEstimated.setRowNumber(serviceOrderDetail.getRowNumber());
					tadvancesReceivedEstimated.setCarName(serviceOrderDetail.getTcar().getCarName());
				}
			}
		}
		return  tadvancesReceived;
	}


	/**
	 * 提交
	 */
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:edit")
	@RequestMapping(value = "updateFinancial")
	@ResponseBody
	public String updateFinancial(String apId ) {
		TadvancesReceivedTransfer tadvancesReceivedTransfer =new TadvancesReceivedTransfer();
		tadvancesReceivedTransfer.setStatus1("1");
		//获得对应的预收转账单
		tadvancesReceivedTransfer.setAdvancesReceivedTransferId(apId);
		tadvancesReceivedTransfer=tadvancesReceivedTransferService.get(tadvancesReceivedTransfer);
		//对应的预收单
		TadvancesReceived tadvancesReceived =new TadvancesReceived();
		tadvancesReceived.setAdvancesReceivedCode(tadvancesReceivedTransfer.getAdvancesReceivedCode());
		tadvancesReceived=tadvancesReceivedService.findList(tadvancesReceived).get(0);

		tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);

		//实际收款表添加
		TadvancesReceivedActual tadvancesReceivedActual=null;
		for (TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail : tadvancesReceivedTransfer.getTadvancesReceivedTransferDetailList()) {
				//出去的钱对应一个子表
				tadvancesReceivedActual =new TadvancesReceivedActual();
				tadvancesReceivedActual.setActualAdvancesReceivedAmount(0-tadvancesReceivedTransferDetail.getCarryOverAmount());
				tadvancesReceivedActual.setAdvancesReceivedType(tadvancesReceivedTransferDetail.getOriginalExpenseType());
				tadvancesReceivedActual.setFrameNumber(tadvancesReceivedTransferDetail.getFrameNumber());
				tadvancesReceivedActual.setAdvancesReceivedId(tadvancesReceived);
				tadvancesReceivedActual.setRowNumber(tadvancesReceivedTransferDetail.getRowNumber());
				tadvancesReceivedService.insetDe(tadvancesReceivedActual);

				//进来的钱对应一个子表
				tadvancesReceivedActual =new TadvancesReceivedActual();
				tadvancesReceivedActual.setActualAdvancesReceivedAmount(tadvancesReceivedTransferDetail.getCarryOverAmount());
				tadvancesReceivedActual.setAdvancesReceivedType(tadvancesReceivedTransferDetail.getExpenseType());
				tadvancesReceivedActual.setFrameNumber(tadvancesReceivedTransferDetail.getCarryOverFrameNumber());
				tadvancesReceivedActual.setAdvancesReceivedId(tadvancesReceived);
			    tadvancesReceivedActual.setRowNumber(tadvancesReceivedTransferDetail.getRowNumber());
				tadvancesReceivedService.insetDe(tadvancesReceivedActual);

				//找到对应的应收单详情
				TadvancesReceivedEstimated tadvancesReceivedEstimated=tadvancesReceivedService.findDetail(tadvancesReceived.getAdvancesReceivedId(),tadvancesReceivedTransferDetail.getOriginRowNumber(),tadvancesReceivedTransferDetail.getOriginalExpenseType());
				//应收详情的未核销金额、已核销金额设置
				tadvancesReceivedEstimated.setReceivedAmountEstimated(tadvancesReceivedEstimated.getReceivedAmountEstimated()-tadvancesReceivedTransferDetail.getCarryOverAmount());
				if(tadvancesReceivedEstimated.getReceivedAmountActual()==null){
					tadvancesReceivedEstimated.setReceivedAmountActual(tadvancesReceivedTransferDetail.getCarryOverAmount());
				}else {
					tadvancesReceivedEstimated.setReceivedAmountActual(tadvancesReceivedEstimated.getReceivedAmountActual()+tadvancesReceivedTransferDetail.getCarryOverAmount());
				}
				//释放锁定金额
			    //tadvancesReceivedEstimated.setUsedAmount(tadvancesReceivedEstimated.getUsedAmount()+tadvancesReceivedTransferDetail.getCarryOverAmount());
			    tadvancesReceivedEstimated.setLockedAmount(tadvancesReceivedEstimated.getLockedAmount()-tadvancesReceivedTransferDetail.getCarryOverAmount());
			    //保存应收详情
				tadvancesReceivedService.updateDetail(tadvancesReceivedEstimated);
				//判断新的收款类型有没有
				boolean flag =true;
				for (TadvancesReceivedEstimated receivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {

					//有
					if(receivedEstimated.getAdvancesReceivedType().equals(tadvancesReceivedTransferDetail.getExpenseType())&&receivedEstimated.getRowNumber().equals(tadvancesReceivedTransferDetail.getRowNumber())){
						TadvancesReceivedEstimated tadvancesReceivedEstimated1=tadvancesReceivedService.findDetail(tadvancesReceived.getAdvancesReceivedId(),tadvancesReceivedTransferDetail.getRowNumber(),tadvancesReceivedTransferDetail.getExpenseType());
						//未核销金额
						tadvancesReceivedEstimated1.setReceivedAmountEstimated(tadvancesReceivedEstimated1.getReceivedAmountEstimated()+tadvancesReceivedTransferDetail.getCarryOverAmount());
						//付款合计
						tadvancesReceivedEstimated1.setReceivedSumActual(tadvancesReceivedEstimated1.getReceivedSumActual()+tadvancesReceivedTransferDetail.getCarryOverAmount());
						//可用金额
						tadvancesReceivedEstimated1.setUsedAmount(tadvancesReceivedEstimated1.getUsedAmount()+tadvancesReceivedTransferDetail.getCarryOverAmount());
						//保存应收单
						tadvancesReceivedService.updateDetail(tadvancesReceivedEstimated1);
						flag=false;
						break;
					}
				}
				// 没有
				if(flag){
					//创建新的应收详情
					TadvancesReceivedEstimated tadvancesReceivedEstimated1 =new TadvancesReceivedEstimated();
					tadvancesReceivedEstimated1.setFrameNumber(tadvancesReceivedTransferDetail.getCarryOverFrameNumber());
					tadvancesReceivedEstimated1.setAdvancesReceivedType(tadvancesReceivedTransferDetail.getExpenseType());
					tadvancesReceivedEstimated1.setAdvancesReceivedId(tadvancesReceived);
					tadvancesReceivedEstimated1.setReceivedSumActual(tadvancesReceivedTransferDetail.getCarryOverAmount());
					tadvancesReceivedEstimated1.setReceivedAmountActual(0.0);
					tadvancesReceivedEstimated1.setReceivedAmountEstimated(tadvancesReceivedTransferDetail.getCarryOverAmount());
					tadvancesReceivedEstimated1.setCarName1(tadvancesReceivedTransferDetail.getCarName());
					tadvancesReceivedEstimated1.setRowNumber(tadvancesReceivedTransferDetail.getRowNumber());
					//可用余额
					tadvancesReceivedEstimated1.setUsedAmount(tadvancesReceivedTransferDetail.getCarryOverAmount());
					//锁定金额
					tadvancesReceivedEstimated1.setLockedAmount(0.0);
					//保存应收详情
					tadvancesReceivedService.insertDetail(tadvancesReceivedEstimated1);
				}
		}
		tadvancesReceivedTransfer.setBillStatus("3");
		tadvancesReceivedTransferService.update(tadvancesReceivedTransfer);
		return "操作成功";
	}

	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:edit")
	@RequestMapping(value = "updateFinancial1")
	@ResponseBody
	public String updateFinancial(String apId,String sta ) {

		TadvancesReceivedTransfer tadvancesReceivedTransfer =new TadvancesReceivedTransfer();
		tadvancesReceivedTransfer.setAdvancesReceivedTransferId(apId);
		tadvancesReceivedTransfer =tadvancesReceivedTransferService.get(tadvancesReceivedTransfer);
		tadvancesReceivedTransfer.setBillStatus(sta);
		tadvancesReceivedTransferService.update(tadvancesReceivedTransfer);
		return "操作成功";
	}
	//作废
	@RequiresPermissions("prerecitran:tadvancesReceivedTransfer:edit")
	@RequestMapping(value = "zuofei")
	@ResponseBody
	public String zuofei(String apId) {

		TadvancesReceivedTransfer tadvancesReceivedTransfer =new TadvancesReceivedTransfer();
		//查出对应的应收单
		TadvancesReceived tadvancesReceived =new TadvancesReceived();
		tadvancesReceived.setAdvancesReceivedCode(tadvancesReceivedTransfer.getAdvancesReceivedCode());
		tadvancesReceived=tadvancesReceivedService.findList(tadvancesReceived).get(0);
		tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);
		//释放锁定金额
		for (TadvancesReceivedTransferDetail tadvancesReceivedTransferDetail : tadvancesReceivedTransfer.getTadvancesReceivedTransferDetailList()) {
			for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {
				if(tadvancesReceivedEstimated.getRowNumber().equals(tadvancesReceivedTransferDetail.getRowNumber())){
					tadvancesReceivedEstimated.setLockedAmount(tadvancesReceivedEstimated.getLockedAmount()-tadvancesReceivedTransferDetail.getCarryOverAmount());
					tadvancesReceivedEstimated.setUsedAmount(tadvancesReceivedEstimated.getUsedAmount()+tadvancesReceivedTransferDetail.getCarryOverAmount());
				}
			}
		}
		tadvancesReceivedTransfer.setStatus1("2");
		tadvancesReceivedService.save(tadvancesReceived);
		tadvancesReceivedTransferService.update(tadvancesReceivedTransfer);
		return "操作成功";
	}
}